---
title: "ME_ExpertSurvey_JC"
author: "Joon H. Chung"
date: "2024-11-04"
output:
  pdf_document: default
  html_document: default
---

```{r, warning=FALSE}
me <- read.csv("me.csv")
```

```{r, warning=FALSE}
library(tidyverse)
library(ggplot2)
library(ggrepel)
library(sjPlot)
library(gapminder)
library(ggridges)
library(broom)
library(dplyr)
library(ggeffects)
library(knitr)
library(kableExtra)
library(janitor)
library(scales)
library(vtable)
```


```{r, warning=FALSE}
## Descriptive Statistics

# Gender variable omitted, as all respondents are male

# Data Cleaning
me$Rank <- rep(NA, nrow(me))
me$Hometown <- rep(NA, nrow(me))
me$Age <- rep(NA, nrow(me))
me$hawk <- rep(NA, nrow(me))

me$Rank[me$rank == 0] <- "Major"
me$Rank[me$rank == 1] <- "Lt.Colonel"

me$Hometown[me$hometown == 0] <- "Rest"
me$Hometown[me$hometown == 1] <- "Gyeongsang"
me$Hometown[me$hometown == 2] <- "Jeolla"

me$Age[me$age == 0] <- "Thirties"
me$Age[me$age == 1] <- "Fourties"
me$Age[me$age == 2] <- "Fifties"

me$hawk[me$militant_assertive == -3 | me$militant_assertive == -2 | me$militant_assertive == -1] <- "Dove"
me$hawk[me$militant_assertive == 3 | me$militant_assertive == 2 | me$militant_assertive == 1] <- "Hawk"
me$hawk[me$militant_assertive == 0] <- "Neutral"

demo <- data.frame(me$Age)
colnames(demo) <- "Age"
demo$Rank <- me$Rank
demo$Military_Year <- me$mil_yr
demo$Field_Year <- me$field_yr
demo$Hometown <- me$Hometown
demo$Hawkishness <- me$hawk


# Categorical Variables
demo1 <- demo %>% 
  clean_names () %>% 
  select_if(is.character)

demo_long <- demo1 %>% 
  pivot_longer(age:hawkishness, names_to = "Demographics") %>% 
  count(Demographics, value) %>%
  group_by(Demographics) %>% 
  mutate(Percentage = percent(n/sum(n)))

demo_long %>% 
  kbl(col.names = c("Demographics", "Value", "n", "%")) %>% 
  kable_styling(full_width = F) %>% 
  column_spec(1, bold = T) %>% 
  collapse_rows(columns = c(1,2), valign = "top")


# Continuous Variables

demo2 <- demo %>% 
  clean_names () %>% 
  select_if(is.numeric)

st(demo2)
```

```{r, warning=FALSE}
## Survey Results: Response Percentages and Histograms
# Response Percentages (7-point Likert scale changed to 3 levels)

me$Order_Compliance <- rep(NA, nrow(me))
me$Discretion <- rep(NA, nrow(me))
me$Basic_Fitness <- rep(NA, nrow(me))
me$Shooting_Skills <- rep(NA, nrow(me))
me$Risk_Own_Life <- rep(NA, nrow(me))
me$Risk_Subordinates <- rep(NA, nrow(me))
me$Interunit_Cooperation <- rep(NA, nrow(me))
me$Trust_Subordinates <- rep(NA, nrow(me))

me$Order_Compliance[me$me1 == -3 | me$me1 == -2 | me$me1 == -1] <- "Unimportant"
me$Order_Compliance[me$me1 == 0] <- "Neither Important nor Unimportant"
me$Order_Compliance[me$me1 == 1 | me$me1 == 2 | me$me1 == 3] <- "Important"

me$Discretion[me$me2 == -3 | me$me2 == -2 | me$me2 == -1] <- "Unimportant"
me$Discretion[me$me2 == 0] <- "Neither Important nor Unimportant"
me$Discretion[me$me2 == 1 | me$me2 == 2 | me$me2 == 3] <- "Important"

me$Basic_Fitness[me$me3 == -3 | me$me3 == -2 | me$me3 == -1] <- "Unimportant"
me$Basic_Fitness[me$me3 == 0] <- "Neither Important nor Unimportant"
me$Basic_Fitness[me$me3 == 1 | me$me3 == 2 | me$me3 == 3] <- "Important"

me$Shooting_Skills[me$me4 == -3 | me$me4 == -2 | me$me4 == -1] <- "Unimportant"
me$Shooting_Skills[me$me4 == 0] <- "Neither Important nor Unimportant"
me$Shooting_Skills[me$me4 == 1 | me$me4 == 2 | me$me4 == 3] <- "Important"

me$Risk_Own_Life[me$me5 == -3 | me$me5 == -2 | me$me5 == -1] <- "Unimportant"
me$Risk_Own_Life[me$me5 == 0] <- "Neither Important nor Unimportant"
me$Risk_Own_Life[me$me5 == 1 | me$me5 == 2 | me$me5 == 3] <- "Important"

me$Risk_Subordinates[me$me6 == -3 | me$me6 == -2 | me$me6 == -1] <- "Unimportant"
me$Risk_Subordinates[me$me6 == 0] <- "Neither Important nor Unimportant"
me$Risk_Subordinates[me$me6 == 1 | me$me6 == 2 | me$me6 == 3] <- "Important"

me$Interunit_Cooperation[me$me7 == -3 | me$me7 == -2 | me$me7 == -1] <- "Unimportant"
me$Interunit_Cooperation[me$me7 == 0] <- "Neither Important nor Unimportant"
me$Interunit_Cooperation[me$me7 == 1 | me$me7 == 2 | me$me7 == 3] <- "Important"

me$Trust_Subordinates[me$me8 == -3 | me$me8 == -2 | me$me8 == -1] <- "Unimportant"
me$Trust_Subordinates[me$me8 == 0] <- "Neither Important nor Unimportant"
me$Trust_Subordinates[me$me8 == 1 | me$me8 == 2 | me$me8 == 3] <- "Important"

dv <- select(me, c("Order_Compliance", "Discretion", "Basic_Fitness", "Shooting_Skills", "Risk_Own_Life", "Risk_Subordinates", "Interunit_Cooperation", "Trust_Subordinates"))

dv_long <- dv %>% 
  pivot_longer(Order_Compliance:Trust_Subordinates, names_to = "Indicators") %>% 
  count(Indicators, value) %>%
  group_by(Indicators) %>% 
  mutate(Percentage = percent(n/sum(n)))

dv_long %>% 
  kbl(col.names = c("Indicators", "Value", "n", "%")) %>% 
  kable_styling(full_width = F) %>% 
  column_spec(1, bold = T) %>% 
  collapse_rows(columns = c(1), valign = "top")

# Ridgeline Plot

me$physical <- (me$me3 + me$me4) / 2
me$risk <- (me$me5 + me$me6) / 2
me$trust <- (me$me7 + me$me8) / 2

main <- data.frame(me$me1)
colnames(main) <- "Order_Compliance"
main$Discretion <- me$me2
main$Physical_Fitness <- me$physical
main$Risk <- me$risk
main$Cooperation <- me$trust

grp_names <- data.frame(`Obey_Order` <- "Order Compliance", `Discretion` <- "Discretion", `Physical_Fitness` <- "Physical Fitness", `Risk` <- "Risk Taking", `Cooperation` <- "Trust")

set_theme(base = theme_bw())

main %>%
  pivot_longer(cols = Order_Compliance:Cooperation) %>%
  ggplot() + 
  geom_density_ridges(mapping = aes(x = value, 
                                    y = name, 
                                    fill = name), 
                      color = "white") + 
  scale_fill_brewer(palette = "Set3", .8) + 
  scale_y_discrete(labels = as_labeller(grp_names)) +
  xlim(-3, 4) +
  scale_x_continuous(name = "Response Distribution", 
                   breaks = c(-3, 4), 
                   labels = c("Invalid", "Valid")) +
  guides(color = "none", fill = "none") + 
  labs(x = "", y = "Indicators", 
       title = "Conceptual Validity of the Indicators of Military Cohesion")

```
